﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TA.Turbo;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	public class MultiScenarioValue : MultiPartValue
	{
		internal MultiScenarioValue(Control owner)
		{
			_owner = owner;
		}
		public List<PostScenario> ScenarioList
		{
			get
			{
				if (_scenarios == null)
				{
					_scenarios = new List<PostScenario>();
					string[] scenarioIds = (this._value ?? "").Split(this.Separators, StringSplitOptions.RemoveEmptyEntries);
					foreach (string scenarioId in scenarioIds)
						_scenarios.Add(this._owner.FindControlGlobal(scenarioId) as PostScenario);
				}
				return _scenarios;
			}
		}
		public override string Value
		{
			get
			{
				if (_scenarios != null)
				{
					StringBuilder sb = new StringBuilder();
					foreach (PostScenario crg in this._scenarios)
					{
						if (sb.Length > 0) sb.Append(Separators[0]);
						sb.Append(crg.UniqueID.Replace(this._owner.UniqueID.Replace(this._owner.ID, ""), ""));
					}
					_value = sb.ToString();

					NullifyLists();
				}
				return base.Value;
			}
			set
			{
				base.Value = value;
			}
		}

		protected override void NullifyLists()
		{
			base.NullifyLists();
			_scenarios = null;
		}
		List<PostScenario> _scenarios;
		Control _owner;
	}
}

